package com.yuexia.sell.controller;

import com.yuexia.sell.dataOjbect.ProductCategory;
import com.yuexia.sell.dataOjbect.ProductInfo;
import com.yuexia.sell.service.Interface.ProductCategoryService;
import com.yuexia.sell.service.Interface.ProductInfoService;
import com.yuexia.sell.util.ReultVoUtil;
import com.yuexia.sell.vo.ProductInfoVo;
import com.yuexia.sell.vo.ResultVo;
import com.yuexia.sell.vo.productVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author itmrchen
 * @date 2018/7/11 0011 上午 9:29
 * @desc ctrate by YUE XIA
 */
@RestController
@RequestMapping("buyer/product")
public class BuyerProductController {
    @Autowired
    private ProductCategoryService productCategoryService;

    @Autowired(required = false)
    private ProductInfoService productInfoService;

    @RequestMapping("list")
    public ResultVo list(){
//        1.查询上架的商品
        List<ProductInfo> productInfoList = productInfoService.findUpAll();

//        2.根据上架商品中的类别查询出所有类目
        //传统方法
        List<Integer> integerListlist = new ArrayList<>();
        for (ProductInfo p : productInfoList) {
            integerListlist.add(p.getCategoryType());
        }

        //jdk1.8新特征方式lamdba
        List<Integer> collect = productInfoList.stream().map(e -> e.getCategoryType()).collect(Collectors.toList());


        List<ProductCategory> productCategoryList = productCategoryService.findByCategoryTypeIn(integerListlist);

//        3.拼装数据
        List<productVo> productVoList = new ArrayList<>();
        for (ProductCategory productCategory:productCategoryList) {
            productVo productVo = new productVo();
            productVo.setCategoryName(productCategory.getCategoryName());
            productVo.setCategoryType(productCategory.getCategoryType());

            List<ProductInfoVo> ProductInfoVoList = new ArrayList<>();
            for (ProductInfo productInfo:productInfoList) {
                if (productCategory.getCategoryType().equals(productInfo.getCategoryType())){
                    ProductInfoVo productInfoVo = new ProductInfoVo();

//                    productInfoVo.setProductId(productInfo.getProductId());
//                    productInfoVo.setProductDescription(productInfo.getProductDescription());
//                    productInfoVo.setProductIcon(productInfo.getProductIcon());
//                    productInfoVo.setProductName(productInfo.getProductName());
//                    productInfoVo.setProductPrice(productInfo.getProductPrice());

                    BeanUtils.copyProperties(productInfo,productInfoVo);

                    ProductInfoVoList.add(productInfoVo);
                }
            }

            productVo.setCategoryFoods(ProductInfoVoList);

            productVoList.add(productVo);
        }

        return ReultVoUtil.success(productVoList);

    }
}
